﻿@model IPageable<XPressify.Models.ThreadListModel>
@using Omu.Awesome.Core;
@using Omu.Awesome.Mvc.Helpers;
@using XPressify.Controllers;

@{
    ViewBag.Title = "Messages";
    Layout = "~/Views/Shared/_Layout.cshtml";
}


<script type="text/javascript">

    function underline(num) {
        var e = document.getElementById('title' + num.toString());
        e.setAttribute("class", "underline-class");
    }

    function dontUnderline(num) {
        var e = document.getElementById('title' + num.toString());
        e.setAttribute("class", "");
    }

    function successDeleteThread(id) {
        $("#" + id + "Div").hide("fast");
    }


</script>


<h2 style="color:#222">Messages</h2>

<p>
    @(Html.MakePopupForm<ThreadController>(o => o.ThreadCreate(), title: "Create", height: 565, width: 600, okText: "Send", resizable: false, refreshOnSuccess: true))
    @(Html.PopupFormActionLink<ThreadController>(o => o.ThreadCreate(), "Create new", new { @class = "nobtn" }))
</p>
<table>

@{ var i = 0;
   var divId = "";
   var successFunc = "";
   foreach (var item in Model.Page)
   {
       divId = item.Id + "Div";
       var name = "title" + i;
        <tr id = "@divId">
            @if (item.IsRead)
            {
                
                <td style="vertical-align:top" width="50px">
                    <a href='@Url.Action("ProfileView", "Profile", new { id = item.LastMessageUser.Id })' class="nobtn">
                        <img src= "@item.ProfilePicturePath" alt="Profile picture" title="Profile picture" style="width: 50px;height: 50px;" />
                    </a>
                </td>
                <td style="vertical-align:top;cursor: pointer;" width="250px" onclick="window.location.href='@Url.Action("CreateMessage", "Message", new { id = item.Id })'" onmouseover="underline(@i)" onmouseout="dontUnderline(@i)">
                    <a href='@Url.Action("ProfileView", "Profile", new { id = item.LastMessageUser.Id })' class="nobtn" style="cursor: pointer;" onmouseover="">
                        @item.LastMessageUser.FullName
                    </a>
                    <br />
                    @item.LastMessageCreated.ToString("dd MMMM, yyyy a't' HH:mm")
                </td>
                <td style="vertical-align:top;cursor: pointer;" onclick="window.location.href='@Url.Action("CreateMessage", "Message", new { id = item.Id })'" onmouseover="underline(@i)" onmouseout="dontUnderline(@i)">
                    <label id="@name" style="font-weight:bold;color:#222;font-size:13px;cursor: pointer;" class="nolink;" >@item.Subject</label> 
                    <br />
                    <label style="color:#222;font-size:13px;cursor: pointer;">@item.LastMessageBody</label> 
                </td>
                <td width="15px">
                    @Html.Confirm("Are you sure you want to delete this message?", "myconfirmBtn", title: "Delete message", yesText: "Delete")   
                    @using (Html.BeginForm("ThreadDelete", "Thread", FormMethod.Post, new { style = "width:20px" }))
                    {
                            <input type="hidden" value="@item.Id" name="Id" />
                            <input type="submit" value="x" class="myconfirmBtn" id="nounderline" onmouseover="changeIdToObject(this)" onmouseout="changeIdToObject(this)"/>
                    }
                </td>
            }
            else
            {
                <th style="vertical-align:top" width="50px">
                    <a href='@Url.Action("ProfileView", "Profile", new { id = item.LastMessageUser.Id })' class="nobtn">
                        <img src= "@item.ProfilePicturePath" alt="Profile picture" title="Profile picture" style="width: 50px;height: 50px;" />
                    </a>
                </th>
                <th style="vertical-align:top;cursor: pointer;" width="250px" onclick="window.location.href='@Url.Action("CreateMessage", "Message", new { id = item.Id })'" onmouseover="underline(@i)" onmouseout="dontUnderline(@i)">
                    <a href='@Url.Action("ProfileView", "Profile", new { id = item.LastMessageUser.Id })' class="nobtn" style="cursor: pointer;" onmouseover="">
                        @item.LastMessageUser.FullName
                    </a>
                    <br />
                    @item.LastMessageCreated
                </th>
                <th style="vertical-align:top;cursor: pointer;" onclick="window.location.href='@Url.Action("CreateMessage", "Message", new { id = item.Id })'" onmouseover="underline(@i)" onmouseout="dontUnderline(@i)">
                    <label id="@name" style="font-weight:bold;color:#222;font-size:13px;cursor: pointer;" class="nolink;" >@item.Subject</label> 
                    <br />
                    <label style="color:#222;font-size:13px;cursor: pointer;">@item.LastMessageBody</label> 
                </th>
                <th width="15px">
                    @{successFunc = "successDeleteThread(\'" + item.Id.ToString() + "\')";}
                    @Html.Confirm("Are you sure you want to delete this message?", "myconfirmBtn", title: "Delete message", yesText: "Delete")   
                    @using (Ajax.BeginForm("ThreadDelete", "Thread", new AjaxOptions() { OnSuccess = successFunc, HttpMethod = "POST", UpdateTargetId = "nothing", InsertionMode = InsertionMode.Replace }))
                    {
                            <input type="hidden" value="@item.Id" name="Id" />
                            <input type="submit" value="x" class="myconfirmBtn" id="nounderline" onmouseover="changeIdToObject(this)" onmouseout="changeIdToObject(this)"/>
                    }
                </th>
            }
            
            
        </tr>
            i++;

   }
}
</table>
<div id = "nothing"></div>
@Html.Pagination()
